package com.tutucms.service.configuration;

import com.tutucms.service.cache.CacheService;
import com.tutucms.service.cache.impl.RedisCacheService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * Created by wangxudong on 2020/10/28.
 *
 * @version: 1.0
 * @modified :
 */
@Configuration
public class CacheServiceConfig {

    @Value("${app.cache.key-prefix:tutu}")
    private String keyPrefix;

    @Bean
    @ConditionalOnBean(RedisTemplate.class)
    @ConditionalOnMissingBean(CacheService.class)
    public CacheService redisCacheService(RedisTemplate<String, Object> redisTemplate) {

        RedisCacheService redisCacheService = new RedisCacheService();
        redisCacheService.setKeyPrefix(keyPrefix);
        redisCacheService.setRedisTemplate(redisTemplate);

        return redisCacheService;
    }
}
